<?php

    /**
     *      [Discuz!] (C)2001-2099 Comsenz Inc.
     *      This is NOT a freeware, use is subject to license terms
     *      $Id: table_mobile_setting.php 31281 2012-08-03 02:29:27Z zhangjie $
     */

    if (!defined('IN_DISCUZ')) {
        exit('Access Denied');
    }

    class table_cloud_app_function extends discuz_table {

        public function __construct() {
            $this->_table = 'cloud_app_function';
            $this->_pk = 'id';

            parent::__construct();
        }

        //插入数据库
        public function insert($arr) {
            return DB::insert($this->_table, $arr, true);
        }

        //更新数据库
        public function update($id, $data) {
            $id = intval($id);
            if (!$data) {
                return;
            }
            return DB::update($this->_table, $data, DB::field($this->_pk, $id));
        }

        //查找某条记录
        public function fetch_app_function_info($id) {
            $id = intval($id);
            return DB::fetch_first("SELECT * FROM %t WHERE id=%d", array($this->_table, $id));
        }

        //查询所有limt数据
        public function app_function_all_find($caid,$start = 0, $limit = 0) {
            return DB::fetch_all('SELECT af.*, f.pid as fpid,f.name,f.linkurl,f.sort,f.icon,f.status,f.ftype FROM ' . DB::table($this->_table) .' as af inner join '.DB::table('cloud_function').' as f on af.fid=f.id where  af.caid='.$caid.
            ' ORDER BY  f.sort asc ' . DB::limit($start, $limit));
        }

        public function app_function_all_not_limt($caid) {
            return DB::fetch_all('SELECT * FROM ' . DB::table($this->_table) . ' as af inner join '.DB::table('cloud_function').' as f on af.fid=f.id where  af.caid='.$caid.
            ' ORDER BY  f.sort asc ');
        }

        public function app_function_all_not_ftype($caid) {
            return DB::fetch_all('SELECT af.*, f.id as fid,f.pid as fpid,f.name,f.linkurl,f.sort,f.icon,f.status,f.ftype FROM ' . DB::table($this->_table) . ' as af inner join '.DB::table('cloud_function').' as f on af.fid=f.id where  af.caid='.$caid.
            ' and f.ftype=2  ORDER BY  f.sort asc ');
        }
        //查询所有limt数据
        public function app_function_all_count($caid) {
            return DB::fetch_first('SELECT count(id) as countnum FROM ' . DB::table($this->_table) . '  where  caid = '.$caid);
        }


        //根据应用名称查询
        public function app_function_all_delete($caid) {
            return DB::query('DELETE FROM ' . DB::table($this->_table) . '  where  caid='.$caid.'');
        }


        public function app_function_info($id) {
            return DB::fetch_first('SELECT af.*, f.pid as fpid,f.name,f.linkurl,f.sort,f.icon,f.status,f.ftype  FROM ' . DB::table($this->_table) . ' as af inner join '.DB::table('cloud_function').' as f on af.fid=f.id where  af.id='.$id.
            ' ORDER BY  f.sort asc ');
        }

        public function app_function_fpid_info($id) {
            return DB::fetch_first('SELECT af.*, f.pid as fpid,f.name,f.linkurl,f.sort,f.icon,f.status,f.ftype  FROM ' . DB::table($this->_table) . ' as af inner join '.DB::table('cloud_function').' as f on af.fid=f.id where  f.id='.$id.
            ' ORDER BY  f.sort asc ');
        }


        public function app_function_by_pid($id,$caid) {
            return DB::fetch_first('SELECT af.*, f.pid as fpid,f.name,f.linkurl,f.sort,f.icon,f.status,f.ftype  FROM ' . DB::table($this->_table) . ' as af inner join '.DB::table('cloud_function').' as f on af.fid=f.id where  f.id='.$id.
            ' and af.caid = '.$caid.' ORDER BY  f.sort asc ');
        }

        public function app_function_not_select_find($caid) {
            return DB::fetch_all('SELECT * FROM ' . DB::table('cloud_function') . ' where status =1 and id not in (select fid from '. DB::table($this->_table).' where caid='.$caid.') ORDER BY ' . DB::order('sort', 'asc') . ',' . DB::order('createtime', 'desc') );
        }


        public function delete_by_function($id) {
            $arg=DB::fetch_first("SELECT * FROM %t WHERE id=%d", array($this->_table, $id));
            return DB::query('DELETE FROM ' . DB::table($this->_table) . '  where  fid in( SELECT id FROM ' . DB::table('cloud_function') . ' where  pid ='. $arg['fid'].')  or id='.$id);
        }
    }